$(document).ready(function() {
    var clientId = $('#clientId').val() || 0;
    
    // Jeśli edytujemy użytkownika, wstawiamy link do wyświetlenia pola z hasłem.
    if (clientId > 0) {
        // Wstawiamy link.
        $('#password').after('<a href="#" class="change-password">Change Password</a>');
        // Ukrywamy pole do zmiany hasła.
        $('#password').hide();
    }
    
    $('.change-password').click(function() {
        // Pokazujemy pole do zmiany hasła.
        $('#password').show();
        // Usuwamy dodany wcześniej link.
        $(this).remove();
    });
    
    $('.delete-client').click(function() {
        var row = $(this).closest('tr');
        var clientId = $(row).attr('data-client-id');
        
        // Żądamy potwierdzenia usunięcia od użytkownika.
        if (!confirm('Are you sure?')) {
            return false;
        }
        
        $.ajax({
            method: 'post',
            url: 'ajax.do?method=deleteClient',
            data: {
                clientId: clientId
            },
            success: function(result) {
                if (result.trim() === 'OK') {
                    // Ukrywamy, a następnie usuwamy wiersz w tabeli.
                    $(row).hide(200, function() {
                        $(row).remove();
                    })
                }
            }
        });
        
        return false;
    });
    
    var usernameExists = false;
    
    $('#manage-client-form #username').change(function() {
        var username = $(this).val();
        
        $.ajax({
            method: 'post',
            url: 'ajax.do?method=checkUsername',
            data: {
                username: username
            },
            success: function(result) {
                if (result == 'ERROR') {
                    usernameExists = true;
                    alert('This login is used by someone else.');
                } else {
                    usernameExists = false;
                }
            }
        });
    });
    
    $('#manage-client-form').submit(function() {
        var clientId = $('#clientId').val() || 0;
        var username = $('#username').val();
        var password = $('#password').val();
        var firstName = $('#firstname').val();
        var lastName = $('#lastname').val();
        var pesel = $('#pesel').val();
        var address = $('#address').val();
        var errors = new Array();
        
        if (username == '') {
            errors.push('Please enter login.');
        } else if (usernameExists) {
            errors.push('This login is used by someone else.');
        }
        
        if (clientId == 0 && password == '') {
            errors.push('Please enter password.');
        }
        
        if (firstName == '') {
            errors.push('Please enter first name.');
        }
        
        if (lastName == '') {
            errors.push('Please enter last name.');
        }
        
        if (pesel == '') {
            errors.push('Please enter PESEL number.');
        }
        
        if (address == '') {
            errors.push('Please enter address.');
        }
        
        if (errors.length == 0) {
            $.ajax({
                method: 'post',
                url: 'ajax.do?method=manageClient',
                data: {
                    clientId: clientId,
                    username: username,
                    password: password,
                    firstName: firstName,
                    lastName: lastName,
                    pesel: pesel,
                    address: address
                },
                success: function(result) {
                    alert('Client has been saved!');
                    if (clientId > 0) {
                        window.location.replace('client.do?method=list');
                    } else {
                        window.location.replace('login.do');
                    }
                    
                }
            });
        } else {
            displayMessages(errors, '#messages');
        }
        
        return false;
    });
});